﻿using Common;
using DAL.DBUtility;
using Enums;
using Enums.Sys;
using Model.Product;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Product
{
    public class ProductDAL
    {
        /// <summary>
        /// 获取所有的数据信息
        /// </summary>
        /// <returns></returns>
        public List<ProductModel> GetList()
        {
            String sql = @"SELECT * FROM Product_Product WHERE 1=1 AND ViewDel=@ViewDel";
            SqlParameter[] parmer =  {
                new SqlParameter("ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele))
           };
            DataTable dt = DbHelperSQL.Query(sql, parmer).Tables[0];
            List<ProductModel> modelList = ListDataSet.DataSetToIList<ProductModel>(dt).ToList();
            return modelList;
        }
        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="productModel"></param>
        /// <returns></returns>
        public int SaveData(ProductModel productModel) {
            int result =0;
            if (productModel.ID == 0)
               result= Insert(productModel);
            else
                result = Edit(productModel);
            return result;
        }
        /// <summary>
        /// 新建操作
        /// </summary>
        /// <param name="productModel"></param>
        /// <returns></returns>
        public int Insert(ProductModel productModel)
        {

            string sql = @"INSERT INTO Product_Product (Name,CreateDate,CreateLoginId,CreateLoginName,ViewDel) VALUES(@Name,@CreateDate,@CreateLoginId,@CreateLoginName,@ViewDel);select @@IDENTITY;";
            SqlParameter[] parmas ={
                                  new SqlParameter("@Name",productModel.Name),
                                  new SqlParameter("@CreateDate",DateTime.Now),
                                  new SqlParameter("@CreateLoginId",productModel.ChangeLoginId),
                                  new SqlParameter("@CreateLoginName",productModel.CreateLoginName),
                                  new SqlParameter("@ViewDel",productModel.ViewDel)
                                  };

            int result = int.Parse(DbHelperSQL.GetSingle(sql, parmas).ToString()); ;
            return result;

        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public ProductModel GetModel(string id) {
            ProductModel model = new ProductModel();
            if (id== "0")
                return model;
            else { 
            string sql = @"SELECT * FROM Product_Product WHERE ID=@ID";
            SqlParameter[] parmas ={
                                  new SqlParameter("@ID",id)
                                  };
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parmas);
            if (sdr.Read())
            {
                model = CommonModel(sdr);
            }
            sdr.Close();
            return model;
            }
        }
        private ProductModel CommonModel(SqlDataReader sdr)
        {
            ProductModel model = new ProductModel();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
           
            model.ViewDel = PageValidate.ConvertToTrimInt(sdr["ViewDel"]);
            model.Name = PageValidate.ConvertToTrimString(sdr["Name"]);
            model.CreateLoginId = PageValidate.ConvertToTrimInt(sdr["CreateLoginId"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.ChangeLoginId = PageValidate.ConvertToTrimInt(sdr["ChangeLoginId"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
          
            return model;
        }

        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="productModel"></param>
        /// <returns></returns>
        public int Edit(ProductModel productModel)
        {
            string sql = @"UPDATE Product_Product SET Name=@Name,ChangeLoginId=@ChangeLoginId,ChangeLoginName=@ChangeLoginName,ChangeDate=@ChangeDate";
            SqlParameter[] parmas ={
                                  new SqlParameter("@Name",productModel.Name),
                                  new SqlParameter("@ChangeDate",DateTime.Now),
                                  new SqlParameter("@ChangeLoginId",productModel.ChangeLoginId),
                                  new SqlParameter("@ChangeLoginName",productModel.ChangeLoginName) 
                                  };

            return DbHelperSQL.ExecuteSql(sql, parmas);
        }
        /// <summary>
        /// 删除实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultEntity DelModel(string id)
        {
            string sql = @"UPDATE Product_Product SET ViewDel=@ViewDel WHERE ID=@ID";
            SqlParameter[] parameters = {
					new SqlParameter("@ID", id),
                    new SqlParameter("@ViewDel",EnumManagement.GetEnumValue(SysEnum.IsDele))
			};
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));
        }
    }
}
